/** \file    ExtendedFileOpenDlg.cpp
  * \brief   Specialized standard file open dialog for overlay importing.
  * \date    2002 - 2004
  * \author  Pavel Agapov
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/icache/dll/ExtendedFileOpenDlg.cpp,v 1.2 2004/07/23 15:33:50 cscom1r4 Exp $
  */

#include "stdafx.h"
#include <Dlgs.h> //for pspHelp definition
#include "Resource.h"
#include "ExtendedFileOpenDlg.h"
						 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

namespace icache {

IMPLEMENT_DYNAMIC(ExtendedFileOpenDlg, CFileDialog)

///mysterious function, I don't understand a word in it
UINT CALLBACK ExtendedFileOpenDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (hWnd == NULL)
		return 0;

	_AFX_THREAD_STATE * pThreadState = ::AfxGetThreadState();
	if (pThreadState->m_pAlternateWndInit != NULL)	{
		ASSERT_KINDOF(CFileDialog, pThreadState->m_pAlternateWndInit);
		pThreadState->m_pAlternateWndInit->SubclassWindow(hWnd);
		pThreadState->m_pAlternateWndInit = NULL;
	}
	ASSERT(pThreadState->m_pAlternateWndInit == NULL);

	if (message == WM_COMMAND && LOWORD(wParam) == pshHelp)	{
		SendMessage(hWnd, WM_COMMAND, ID_HELP, 0);
		return 1;
	}

	ExtendedFileOpenDlg * pDlg = (ExtendedFileOpenDlg *) CWnd::FromHandlePermanent(hWnd);
	ASSERT(pDlg != NULL);
	ASSERT_KINDOF(ExtendedFileOpenDlg, pDlg);

	if (message == WM_COMMAND)
		pDlg->OnCustomCommand(LOWORD(wParam), HIWORD(wParam));

	return 0;
}

ExtendedFileOpenDlg::ExtendedFileOpenDlg(LPCTSTR lpszDefExt, LPCTSTR lpszFilter, bool allow_still, CWnd* pParentWnd) :
	CFileDialog(TRUE, lpszDefExt, NULL, 
		OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_ENABLESIZING | /*(allow_still ? (*/OFN_EXPLORER | OFN_ENABLETEMPLATE/*) : 0)*/,
		lpszFilter, pParentWnd)
{
	m_bAllowLoadAsStill = allow_still;
	//if (allow_still) {
		m_ofn.Flags |= OFN_ENABLESIZING; //this flag is removed in CFileDialog ctor when it sees OFN_ENABLETEMPLATE flag
		//m_ofn.FlagsEx = OFN_EX_NOPLACESBAR;
		SetTemplate(0, IDD_FILEOPEN_STILL_ADDON);
		m_ofn.lpfnHook = ExtendedFileOpenDlgProc;
	//}
	m_bLoadAsStillImage = false;
	m_DeinterlaceFields = fields_off;
	m_DeinterlaceMethod = method_duplicate;
}

BEGIN_MESSAGE_MAP(ExtendedFileOpenDlg, CFileDialog)
	//{{AFX_MSG_MAP(ExtendedFileOpenDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void ExtendedFileOpenDlg::OnInitDone()
{
	CFileDialog::OnInitDone();

	CButton * pLoadAsStillButton = (CButton *) GetDlgItem(IDC_LOAD_AS_STILL_IMAGE);
	pLoadAsStillButton->SetCheck(m_bLoadAsStillImage ? 1 : 0);

	CComboBox * pFieldsCombo = (CComboBox *) GetDlgItem(IDC_DEINTERLACE_FIELDS);
	pFieldsCombo->AddString("Off");
	pFieldsCombo->AddString("Upper First");
	pFieldsCombo->AddString("Lower First");
	pFieldsCombo->SetCurSel(m_DeinterlaceFields);

	CComboBox * pMethodCombo = (CComboBox *) GetDlgItem(IDC_DEINTERLACE_METHOD);
	pMethodCombo->AddString("Duplicate");
	pMethodCombo->AddString("Interpolate");
	pMethodCombo->SetCurSel(m_DeinterlaceMethod);

	EnableControls();
}

void ExtendedFileOpenDlg::OnCustomCommand(UINT nID, UINT msg)
{
	CButton * pLoadAsStillButton = (CButton *) GetDlgItem(IDC_LOAD_AS_STILL_IMAGE);
	m_bLoadAsStillImage = pLoadAsStillButton->GetCheck() == 1;

	CComboBox * pFieldsCombo = (CComboBox *) GetDlgItem(IDC_DEINTERLACE_FIELDS);
	m_DeinterlaceFields = deinterlace_fields(pFieldsCombo->GetCurSel());

	CComboBox * pMethodCombo = (CComboBox *) GetDlgItem(IDC_DEINTERLACE_METHOD);
	m_DeinterlaceMethod = deinterlace_method(pMethodCombo->GetCurSel());

	EnableControls();
}

void ExtendedFileOpenDlg::EnableControls()
{
	CButton * pLoadAsStillButton = (CButton *) GetDlgItem(IDC_LOAD_AS_STILL_IMAGE);
	pLoadAsStillButton->EnableWindow(m_bAllowLoadAsStill);	

	CComboBox * pFieldsCombo = (CComboBox *) GetDlgItem(IDC_DEINTERLACE_FIELDS);
	pFieldsCombo->EnableWindow(!m_bLoadAsStillImage);

	CComboBox * pMethodCombo = (CComboBox *) GetDlgItem(IDC_DEINTERLACE_METHOD);
	pMethodCombo->EnableWindow(!m_bLoadAsStillImage && m_DeinterlaceFields != fields_off);
}

} //namespace icache
